<?php
include "header.php";
include "mysqli_connection.php";

if (!isset($_SESSION["username"])){
	echo "<script>window.location.href='login.php'</script>";
}
else{
	if (isset($_GET["id"]) && $_GET["id"] !== ""){
		$id = $mysqli->real_escape_string(trim($_GET["id"]));
		if ($id != 0){
			$query = "SELECT prefix, number, title, professor FROM Courses WHERE c_id = ?;";
			$stmt = $mysqli->stmt_init();
			if ($stmt = $mysqli->prepare($query)){
				$stmt->bind_param("i", $id);
				$stmt->execute();
				$stmt->bind_result($prefix, $number, $title, $professor);
				if (!($stmt->fetch())){
					echo "<script>window.location.href='managecourses.php'</script>";
				}
				$stmt->close();
			}
			$button_name = "Update";
		}
		else{
			$button_name = "Add";
		}
	}
	else{
		$id = 0;
		$button_name = "Add";
	}
?>
<script>change_picture("admin");</script>
<div>
<form action = "updatecourses.php" method = "POST"/>
<input type = "hidden" name = "c_id" value = "<?php echo $id; ?>" />
<label for = "prefix">Prefix: </label><input type = "text" name = "prefix" value = "<?php echo $prefix; ?>" /><br />
<label for = "number">Number: </label><input type = "text" name = "number" value = "<?php echo $number; ?>" /><br />
<label for = "title">Title: </label><input type = "text" name = "title" value = "<?php echo $title; ?>" /><br />
<label for = "professor">Professor: </label><input type = "text" name = "professor" value = "<?php echo $professor; ?>" /><br />
<input type = "button" value = "Back" onclick = "window.location.href='managecourses.php'" />
<input type = "submit" value = "<?php echo $button_name;?>"/>
</form>
</div>
<?php
}
include "footer.php";